package com.dxysun.springboot.controller.aop;

import com.dxysun.springboot.controller.HelloController;
import com.dxysun.springboot.dto.MyUser;
import com.dxysun.springboot.service.MyService;
import com.dxysun.springboot.dto.User;
import com.dxysun.springboot.service.api.ParseService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController
{
	private static final Log logger = LogFactory.getLog(MyController.class);

	@Autowired
	private MyService myService;

	@Autowired
	@Qualifier("myParseService")
	private ParseService parseService;

	@Autowired
	private HelloController helloController;

	@RequestMapping("/test1")
	@ResponseBody
	public String test1()
	{
		String str = myService.getServiceInfo("MyController", "abc");
//		System.out.println("MyController:" + str);
		logger.info("test1");

		return "test1";
	}

	@RequestMapping("/test2")
	@ResponseBody
	public String test2()
	{
		myService.asyncTest();
		logger.info("test2");

		return "test2";
	}


	@RequestMapping("/testuser")
	@ResponseBody
	public User getMyInfo(MyUser user)
	{
		String str = myService.getServiceInfo("MyController", user.getName());
		logger.info("MyController:" + str);

		if("error".equals(user.getName()))
		{
			throw new RuntimeException("controller 异常");
		}
//		User myUser = new MyUser();
//		User user1 = helloController.getInfo((MyUser)myUser);
//		logger.info("user1:" + JSON.toJSONString(user1));

		user.setInfo(str);
		return user;
	}

	@RequestMapping("/parse")
	@ResponseBody
	public String parse(@RequestParam(required = false, defaultValue = "my") String name)
	{
		return myService.getParseService(name).parse(name);
	}

	@RequestMapping("/parse1")
	@ResponseBody
	public String parse1(@RequestParam(required = false, defaultValue = "my") String name)
	{
		return parseService.parse(name);
	}

	@RequestMapping("/parse2")
	@ResponseBody
	public String parse2(@RequestParam(required = false, defaultValue = "my") String name)
	{
		return parseService.getName();
	}
}
